package com.sise.dubbo.abtest.service.impl;

import com.sise.dubbo.abtest.service.UserService;
import com.sise.dubbo.abtest.vo.UserVO;
import com.sise.dubbo.api.ABTestUserRpcService;
import com.sise.dubbo.dto.RpcRespDTO;
import com.sise.dubbo.dto.user.UserDTO;
import com.sise.dubbo.util.RpcResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zitian
 * @data 2019/1/12
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private ABTestUserRpcService abTestUserRpcService;

    @Override
    public Boolean batchInsertUserPO(List<UserVO> userVOList) {
        List<UserDTO> userDTOS = new ArrayList<>();
        for (UserVO userVO : userVOList) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userVO, userDTO);
            userDTOS.add(userDTO);
        }
        try {
            RpcRespDTO<Boolean> rpcRespDTO = abTestUserRpcService.batchInsert(userDTOS);
            if (RpcResponseUtils.isSuccess(rpcRespDTO)) {
                return true;
            }
        } catch (Exception e) {
            log.error("[用户服务]远程调用rpc异常");
        }
        return false;
    }

    @Override
    public String print() {
        return String.valueOf(abTestUserRpcService.sayHello());
    }
}
